<?php
/**
 * @author sanketsp
 * Instructor class: Holds data on the instructor currently signed-in into eTAT
 */
class Instructor {
	function __construct($id) {
		$query = mysql_query("SELECT * FROM `instructor` WHERE `id` = '$id';") or die(mysql_error());
		$result = mysql_fetch_object($query);
		if($result) {
			foreach($result as $key=>$value) {
				$this->$key = $value;
			}
		}
	}
	/**
	 * Update instructor data
	 */
	private function updateInstructor() {
		$query = mysql_query("UPDATE `instructor` SET `full_name` = '$this->full_name', `email` = '$this->email', `permission` = '$this->permissiom'") or die(mysql_error());
		return true;
	}
	/**
	 * Get all courses created by this instructor
	 */
	public function getAllCourses() {
		$courses = null;
		$query = mysql_query("SELECT * FROM `course` WHERE `owner_id` = '$this->id'") or die(mysql_error());
		while($r = mysql_fetch_object($query)) {
			$courses[] = new Course($r->id);
		}
		return $courses;
	}
	/**
	 * Get a single course created by this instructor
	 * @param $id requires the course id
	 */
	public function getCourse($id) {
		$courses = $this->getAllCourses();
		foreach($courses as $c) {
			if($id == $c->id)
			$course = $c;
		}
		if($course) {
			return $course;
		} else {
			return false;
		}
	}
	/**
	 * Get a single group belonging to one of the courses created by this instructor
	 * @param $id requires the group id
	 */
	public function getGroup($id) {
		$courses = $this->getAllCourses();
		if($courses) {
			foreach($courses as $c) {
				$groups = $c->getAllGroups();
				if($groups) {
					foreach($groups as $g) {
						if($g->id == $id) {
							return new Group($id);
						}
					}
				} else {
					return false;
				}
			}
		} else {
			return false;
		}
	}
	/**
	 * Adds a new course
	 * @param $data data string required
	 * @param $data->title course title
	 * @param $data->term course term
	 * @param $data->description course description
	 */
	public function addCourse($data) {
		$query = mysql_query("INSERT INTO `course` VALUES (NULL, $this->id , '$data->title', '$data->term', '$data->description')") or die(mysql_error());
		$all = $this->getAllCourses();
		foreach($all as $a) {
			if($a->term == $data->term) {
				$course = $a;
			}
		}
		return $course->id;
	}
	/**
	 * Removes one of the courses created by this instructor
	 */
	public function removeCourse($id) {
		$all = $this->getAllCourses();
		foreach($all as $a) {
			if($a->id == $id) {
				$a->removeOwner($this->id);
				break;
			}
		}
		return true;
	}
	/**
	 * Shares a course with another instructor
	 */
	public function shareCourse($course_id,$instructor_id) {
		$share = new Instructor($instructor_id);
		$course = getCourse($course_id);
		if($course && $share) {
			$course->addOwner($share);
			return true;
		} else {
			return false;
		}
	}
	/**
	 * Returns all the surveys created by this instructor
	 */
	public function getAllSurveys(){
		$query = mysql_query("SELECT * FROM `survey` WHERE `instructor_id` = '$this->id'") or die (mysql_error());
		while($r = mysql_fetch_object($query)) {
			$surveys[] = new Survey($r->id);
		}
		if($surveys) {
			return $surveys;
		} else {
			return false;
		}
	}
	/**
	 * Returns a single survey created by this instructor
	 * @param $id requires the survey id
	 */
	public function getSurvey($id) {
		$query = mysql_query("SELECT * FROM `survey` WHERE `instructor_id` = '$this->id' AND `id` = '$id';") or die(mysql_error());
		$result = mysql_fetch_object($query);
		if($result) {
			return new Survey($result->id);
		} else {
			return false;
		}
	}
	/**
	 * Creates a new survey for this instructor
	 * @param $data survey data string required
	 */
	public function createSurvey($data) {
		$sql = mysql_query("INSERT INTO `survey` VALUES (
			NULL,
			'$this->id',
			'$data->survey_name',
			'$data->survey_title',
			'$data->welcome_message',
			'$data->confirmation_message',
			'$data->email_subject',
			'$data->email_message',
			'$data->date_created',
			'$data->last_modified',
			NULL,
			NULL,
			NULL
		)") or die(mysql_error());
		return true;
	}
	/**
	 * Deletes a survey created by this instructor
	 * @param $id requires survey id
	 */
	public function deleteSurvey($id) {
		mysql_query("DELETE FROM `survey` WHERE `id` = '$id'") or die(mysql_error());
	    mysql_query("DELETE FROM `question` WHERE `survey_id` = '$id'") or die(mysql_error());
		mysql_query("DELETE FROM `response` WHERE `survey_id` = '$id'") or die(mysql_error());
	    mysql_query("DELETE FROM `response_value` WHERE `survey_id` = '$id'") or die(mysql_error());
	}
	/**
	 * Copies a survey created by this instructor
	 * @param $id requires the id of the survey to be copied
	 */
	public function copySurvey($id) {
		$s = $this->getSurvey($id);
		$s->survey_name .= '_copy';
		$query = mysql_query("INSERT INTO `survey` VALUES
			(NULL,
			'$this->id',
			'$s->survey_name',
			'$s->survey_title',
			'$s->welcome_message',
			'$s->confirmation_message',
			'$s->email_subject',
			'$s->email_message',
			'$s->date_created',
			'$s->last_modified',
			'$s->schedule',
			'$s->last_sent',
			'$s->scheduled_to_groups');
		") or die(mysql_error());
		$query = mysql_query("SELECT MAX(`id`) FROM `survey` WHERE `instructor_id`='$this->id'") or die(mysql_error());
		$result = mysql_fetch_row($query);
		$new = $this->getSurvey($result[0]);
		if($new) {
			$questions = $s->getAllQuestions();
			if($questions) {
				foreach($questions as $q) {
					$data = (object) array(
		              'type'=>$q->type,
		              'required'=>$q->required,
		              'question'=>$q->question,
		              'create_section'=>$q->create_section,
		              'order'=>$q->order,
		              'response1'=>$q->response1,
					  'response2'=>$q->response2,
					  'response3'=>$q->response3,
					  'response4'=>$q->response4,
					  'response5'=>$q->response5,
					  'response6'=>$q->response6
					);
					$new->addQuestion($data);
				}
			}
		}
	}
}